package com.tensquare.article.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.core.type.TypeReference;
import com.tensquare.article.controller.CatException;
import com.tensquare.article.dao.ArticleDao;
import com.tensquare.article.pojo.Article;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import util.IdWorker;

import java.util.List;
import java.util.Map;

@Service
public class ArticleService {
    @Autowired(required = false)
    public ArticleDao articleDao;

    @Autowired
    IdWorker idWorker;
    @Autowired
    RedisTemplate redisTemplate;


    //通过文章订阅用户
    // articleId 文章id
    //userId 当前操作用户id
    public boolean articleId(String articleId,String userId){
        //发布集合名称
        String release="Release";
        //关注者集合名称
        String fans="fans";
        //通过文章id获取文章发布者id
        Article byId = findById(articleId);
        //发布者id
        String releaseId = byId.getUserid();
        //查询当前用户的发布者集合是否存在文章发布者
        Boolean member = redisTemplate.opsForSet().isMember(release+userId,releaseId);
        if(member){
            //删除文章发布者的关注者集合中的当前操作用户
            redisTemplate.opsForSet().remove(fans+releaseId,userId);
            //删除当前用户的发布者集合
            redisTemplate.opsForSet().remove(release+userId,releaseId);
            return false;
        }else {
            //不存在添加集合
            //删除文章发布者的关注者集合中的当前操作用户
            redisTemplate.opsForSet().add(fans+releaseId,userId);
            //删除当前用户的发布者集合
            redisTemplate.opsForSet().add(release+userId,releaseId);
            return true;
        }
    }

    //查询所有文章信息
    public List<Article> findAll(){
        List<Article> articles = articleDao.selectList(null);
        return articles;
    }
    //修改文章
    public void update(String id,Article article){
        //先查询id是否正确
        Article article1 = articleDao.selectById(id);
        if(article1==null){
            //抛出异常
            CatException.CatEx(new Result(false, StatusCode.ERROR,"id错误"));
        }
        article.setId(id);

        articleDao.updateById(article);
    }

    //根据id查询文章
    public Article findById(String id){
        Article article = articleDao.selectById(id);
        return article;
    }

    //修改文章
    public void save(Article article){
        //生成分布式id
        String s = idWorker.nextId() + "";
        article.setId(s);
//        private Integer visits;     //浏览量
//        private Integer thumbup;    //点赞数
//        private Integer comment;    //评论数
        //初始化
        article.setVisits(0);
        article.setThumbup(0);
        article.setComment(0);
        //增加文章
        articleDao.insert(article);
    }

    //根据id删除文章
    public void delete(String id){
        articleDao.deleteById(id);
    }

    //根据条件分页查询
    public List<Article> findByPage(Integer page, Integer size,Article article){
        Map<String, Object> map = JSON.parseObject(JSON.toJSONString(article));        //创建查询构造器
        EntityWrapper<Article> employeeEntityWrapper = new EntityWrapper<Article>();
        //设置查询条件
        employeeEntityWrapper.allEq(map);
        //返回数据
        List<Article> articles = articleDao.selectPage(new Page<Article>(page, size), employeeEntityWrapper);
        return articles;
    }

}
